<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Schema extends Schema_Base {
	public static function init() { ?>
		<script type="text/javascript">
		$(function() { Core.Schema.init(); });
		</script>
		<?php
	}

	public static function drawTabs($schema_file) {
		$schema = Data::getSchema($schema_file);
		if ($schema) {
			$cnt = 1;
			foreach ($schema->children() as $form) {
				$title = $form['title'];
				if (isset($form['title_' . Router::getLanguage()])) {
					$title = $form['title_' . Router::getLanguage()];
				} ?>
				<li id="schema_tab_<?php echo $cnt; ?>">
					<a href="#<?php echo $cnt; ?>" onclick="Core.Schema.openTab(<?php echo $cnt; ?>)"><?php echo $title; ?></a>
				</li>
				<?php
				$cnt++;
			}
		}
	}

	public static function drawBlocks($schema_file, $table = '', $column = '', $id = 0) {
		$schema = Data::getSchema($schema_file);
		if ($schema) {
			if ($id > 0) {
				$data = new Data($schema, $table, $column, $id);
				$fields = $data->get();
			}

			$cnt = 1;
			foreach ($schema->children() as $form) { ?>
				<div class="schema_block" id="schema_block_<?php echo $cnt; ?>" style="display:none;">
					<table class="table">
						<colgroup>
							<col class="schema_column_headers" />
							<col class="schema_column_fields" />
						</colgroup>
						<tbody>
						<?php
						foreach ($form->children() as $field) {
							$value = '';
							if ($id > 0) {
								$value = $fields[(string)$field['name']];
							}
							$title = $field['title'];
							if (isset($field['title_' . Router::getLanguage()])) {
								$title = $field['title_' . Router::getLanguage()];
							} ?>
							<tr>
								<td class="schema_cell_header" title="<?php echo $field['name']; ?>">
									<?php
									if (isset($field['lang']) && (strtolower(trim($field['lang'])) == 'true')) {
										$style = ' float:right; margin:4px 0px 0px 5px;';?>
										<img src="<?php echo PUBLIC_URL; ?>images/flag/<?php echo Config::get('schema_current', 'i18n'); ?>.png" style="width:16px; height:11px;<?php echo $style; ?>" />
									<?php } ?>
									<?php echo $title; ?>
								</td>
								<td class="schema_cell_field"><?php echo self::drawField($field, $value); ?></td>
							</tr>
						<?php } ?>
						</tbody>
					</table>
				</div>
				<?php
				$cnt++;
			}
		}
	}

	public static function drawSelect($list, $selected_id = 0) {
		print_r($list);
		foreach ($list as $item) {
			$selected = '';
			if (intval($selected_id) == intval($item['id'])) {
				$selected = ' selected';
			}

			printf('<option value="%d">%s%s</option>',
				$item['id'],
				$selected,
				$item['name']
			);
		}
	}

	public static function drawField($field, $value = '') {
		$result = '';

		$field_type = strtolower(trim($field['type']));

		$field_style = '';

		$field_direction = '';
		if (Config::get('schema_rtl', 'i18n')) {
			$field_direction = 'rtl';
		}
		
		if (isset($field['dir'])) {
			$field_direction = trim(strtolower($field['dir']));
		}

		if (!empty($field_direction)) {
			$field_style .= 'direction:' . $field_direction . ';';
		}


		if (isset($field['style'])) {
			$field_style = $field['style'];
		}

		$field_name = 'data_' . $field['name'];
		$field_id = 'data_' . $field['name'];

		switch ($field_type) {

			// Textbox (single line)
			case 'text':
			case 'textbox':
			case 'textfield':
				$result .= '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . self::clearValue($value) . '" class="schema_textfield" style="' . $field_style . '" />';
				break;

			// Textarea (multi line)
			case 'textarea':
				$html_class = '';
				if (isset($field['html']) && (trim(strtolower($field['html'])) == 'true')) {
					$html_class = ' schema_html';
					if (!empty($field_direction) && ($field_direction == 'rtl')) {
						$html_class = ' schema_html_rtl';
					}
				}
				$result .= '<textarea name="' .$field_name . '" id="' . $field_id . '" class="schema_textarea' . $html_class . '" style="' . $field_style . '">' . self::clearValue($value) . '</textarea>';
				break;

			// Checkbox
			case 'checkbox':
				$result .= '<input type="checkbox" name="' . $field_name . '" id="' . $field_id . '"';
				if (strtolower(trim($value)) == 'on') {
					$result .= ' checked="true"';
				}
				$result .= ' class="schema_checkbox" style="' . $field_style . '" />';
				break;

			// Radio button
			case 'radio':
				$cnt = 0;
				$result .= '<div style="border:1px solid #000; padding:5px;">';
				foreach ($field->children() as $item) {
					$result .= '<div><input type="radio" name="' . $field_name . '" id="' . $field_id . '-' . $cnt . '" value="' . $item['value'] . '"';
					if ($value == $item['value']) {
						$result .= ' checked="true"';
					}
					$result .= ' class="schema_radio" style="' . $field_style . '" />';
					$result .= '<label for="' . $field_id . '-' . $cnt . '" class="schema_radio_text">' . $item['title'] . '</label></div>';
					$cnt++;
				}
				$result .= '</div>';
				break;

			// Select
			case 'select':
				$result .= '<select name="' . $field_name . '" id="' . $field_id . '" class="schema_select" style="' . $field_style . '">';
				foreach ($field->children() as $item) {
					$result .= '<option value="' . $item['value'] . '"';
					if ($value == $item['value']) {
						$result .= ' selected="true"';
					}
					$result .= 'class="schema_select_option">' . $item['title'] . '</option>';
				}
				$result .= '</select>';
				break;

			// File
			case 'file':
				$file_id = 0;
				$file_name = '';
				$file_original_name = '';
				$preview = '';
				$field_prefix = $field_name;

				if (!empty($value)) {
					$file_id = intval($value);
					$file_info = File::getInfo($file_id, 'name,name_thumb,name_orig,extension');
					if (!empty($file_info['name'])) {
						$file_name = $file_info['name'];
						$file_thumb_name = $file_info['name_thumb'];
						$file_original_name = $file_info['name_orig'];
						$file_extension = $file_info['extension'];

						if (in_array($file_extension, Config::get('file_images_extensions'))) {
							if (!is_null($file_thumb_name)) {
								$preview = UPLOAD_URL . $file_thumb_name;
							} else {
								$preview = UPLOAD_URL . $file_name;
							}
						} else {
							$preview = PUBLIC_URL . 'images/file/' . File::getIcon($file_extension);
						}
					} else {
						$file_id = 0;
					}
				}

				$result .= '<input type="hidden" name="' . $field_name . '"  id="' . $field_id . '"value="' . $file_id . '" />';
				$result .= '<div id="' . $field_prefix . '_select" ' . (($file_id) ? ' style="display:none;"' : '') . '><input type="button" value="Select" class="btn" onclick="Core.Schema.fileBrowserOpen(\'' . $field_prefix . '\', 0)" /></div>';
				$result .= '<div id="' . $field_prefix . '_info" ' . ((!$file_id) ? ' style="display:none;"' : '') . '>';
				$result .= '	<a href="' . (UPLOAD_URL . $file_name) . '" id="' . $field_prefix . '_link" target="_blank" class="pull-left" style="margin:0px 10px 0px 0px;">';
				$result .= '		<img src="' . $preview . '" id="' . $field_prefix . '_preview" title="' . $file_original_name . '" align="top" style="width:48px; height:48px; border:none;" />';
				$result .= '	</a>';
				$result .= '	<div style="padding-bottom:1px;"><input type="button" name="' . $field_prefix . '_change" value="Change" class="btn btn-small"onclick="Core.Schema.fileBrowserOpen(\'' . $field_prefix . '\', 0)" /></div>';
				$result .= '	<div><input type="button" name="' . $field_prefix . '_remove" value="Remove" class="btn btn-small"onclick="Core.Schema.removeFile(\'' . $field_prefix . '\')" /></div>';
				$result .= '</div>';
				break;

			// Simple file
			case 'file_simple':
				$result .= '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . self::clearValue($value) . '" class="schema_file_simple" style="' . $field_style . '" ondblclick="Core.Schema.fileBrowserOpen(\'' . $field_id . '\', 1)" autocomplete="off" />';
				$result .= '<img src="' . PUBLIC_URL . 'images/preview.png" style="margin:4px 0px 0px 5px; cursor:pointer;" onclick="Core.Schema.previewFile(\'' . $field_id . '\')" />';
				break;

			// Date picker
			case 'date':
				$result .= '<input type="text" name="' . $field_name . '" id="' . $field_id . '" value="' . self::clearValue($value) . '" class="schema_date" style="' . $field_style . '" autocomplete="off" />';
				break;

			default:
				$result .= 'Field type "' . $field_type . '" not implemented yet';
		}

		echo $result;
	}

	public static function saveFields($schema, $table, $column, $id, $new = false) {
		$data = new Data($schema, $table, $column, $id);
		$data->save($new);
	}
}
